	;; these routines are located in both MAINPLAY and SUBPLAY
	;; NOTE: edit MAINPLAY/DIVIDE.ASM and not SUBPLAY/DIVIDE.ASM

	dc.b	$22,$2F,$00,$04,$20,$2F,$00,$08

	;; FX arg:d0.l dividend
	;; FX arg:d1.l divisor
	;; FX returns d0.l long word quotient
SignedDivideLongWordByLongWord:
	;; Label16C0:
	LEA      SignedDivideLongWordByLongWord2(PC),A0
	MOVEA.W  #$2,A1
	;; always jump to SignedDivideLongWordByLongWord2
	;; 	JMP      -$2(A0,A1.W) compiles to $4ef090fe
	dc.l	$4ef092fe
SignedDivideLongWordByLongWord2:	
	BRA.S   SignedDivideLongWordByLongWord3
	;; unused data
        dc.w	$4C41,$0801,$4E75
SignedDivideLongWordByLongWord3:	
	JSR      SignedDivideLongWordByLongWordCommon(PC)
	;; move quotient from d1 into d0
	MOVE.L   D1,D0
	RTS

	dc.b	$22,$2F,$00,$04,$20,$2F,$00,$08

	;; FX arg:d0.l dividend
	;; FX arg:d1.l divisor
	;; FX returns d0.w word remainder
	;; FX returns d1.l long word quotient (not used though it could be)
SignedDivideRemainderLongWordByLongWord:	
	LEA      SignedDivideRemainderLongWordByLongWord2(PC),A0
	MOVEA.W  #$2,A1
	;; always jump to SignedDivideRemainderLongWordByLongWord2
	;; 	JMP      -$2(A0,A1.W) compiles to $4ef090fe
	dc.l	$4ef092fe
SignedDivideRemainderLongWordByLongWord2:
	;; this will end with a RTS that will return to
	;; caller of SignedDivideRemainderLongWordByLongWord
	BRA.S    SignedDivideLongWordByLongWordCommon
	;; unused data
	dc.w	$4C41,$0801,$C340,$4E75

SignedDivideLongWordByLongWordCommon:	
	TST.L    D0
	BMI.S    NegativeD0
	TST.L    D1
	BMI.S    PositiveD0NegativeD1
	JSR      UnsignedDivideLongWordByLongWordCommon(PC)
	RTS

PositiveD0NegativeD1:	
	NEG.L    D1
	JSR      UnsignedDivideLongWordByLongWordCommon(PC)
	NEG.L    D1
	RTS

NegativeD0:	
	NEG.L    D0
	TST.L    D1
	BMI.S    NegativeD0NegativeD1
	JSR      UnsignedDivideLongWordByLongWordCommon(PC)
	NEG.L    D0
	NEG.L    D1
	RTS

NegativeD0NegativeD1:	
	NEG.L    D1
	JSR      UnsignedDivideLongWordByLongWordCommon(PC)
	NEG.L    D0
	RTS

	dc.b	$22,$2F,$00,$04,$20,$2F,$00,$08

	;; unused subroutine
UnsignedDivideLongWordByLongWord:
	LEA      UnsignedDivideLongWordByLongWord2(PC),A0
	MOVEA.W  #$2,A1
	;; always jump to UnsignedDivideLongWordByLongWord2
	;; 	JMP      -$2(A0,A1.W) compiles to $4ef090fe
	dc.l	$4ef092fe
UnsignedDivideLongWordByLongWord2:
	BRA.S   UnsignedDivideLongWordByLongWordCommon
	;; unused data
	dc.w	$4C41,$0001,$C340,$4E75

UnsignedDivideLongWordByLongWordCommon:
	MOVE.W   D1,-(A7)
	SWAP     D1
	TST.W    D1
	BNE.S    UnsignedDivideLongWordByNonZeroLongWord

	;; unsigned divide by word (upper word was zero)
	;; move upper word of d0 to lower word of d1
	MOVE.L   D0,D1
	CLR.W    D1
	SWAP     D1
	;; UnsignedDivideWordByWord
	BEQ.S    UnsignedDivideWordByWord
	;; unsigned divide long word by word (upper word was non-zero)
	DIVU.W   (A7),D1
	;; put quotient in upper word of d1 and upper word of d0
	SWAP     D1
	SWAP     D0
	MOVE.W   D1,D0
	SWAP     D0
UnsignedDivideWordByWord:	
	;; restore stack and do division
	DIVU.W   (A7)+,D0
	;; put quotient in lower word of d1 (so have long word quotient)
	;; put remaider in lower word of d0
	MOVE.W   D0,D1
	CLR.W    D0
	SWAP     D0
	RTS

UnsignedDivideLongWordByNonZeroLongWord:
	;; previously executed instructions before branch here...
	;; MOVE.W   D1,-(A7)
	;; SWAP     D1
	;; TST.W    D1
	;; undo previous SWAP of d1
	SWAP     D1
	;; save d2 and d3 on stack (note, d1.w already on stack, override this)
	MOVE.W   D2,(A7)
	MOVE.L   D3,-(A7)
	;; setup the divide operation
	MOVE.W   D0,D2
	MOVE.L   D1,D3
	MOVEQ    #$1,D1
	CLR.W    D0
	SWAP     D0
	BNE.S    UnsignedDivideLongWordByLongWordLoopBody
	;; since upper word is zero, cannot even divide once
	;; undo swap (which clears upper word) and put in value for remainder
	SWAP     D0
	MOVE.W   D2,D0
	;; set zero for quotient
	MOVEQ    #$0,D1
	BRA.S    UnsignedDivideLongWordByLongWordEnd
UnsignedDivideLongWordByLongWordLoopCondition:	
	ADD.W    D1,D1
	BCS.S    UnsignedDivideLongWordByLongWordEnd
UnsignedDivideLongWordByLongWordLoopBody:	
	ADD.W    D2,D2
	ADDX.L   D0,D0
	CMP.L    D3,D0
	BCS.S    UnsignedDivideLongWordByLongWordLoopCondition
	SUB.L    D3,D0
	ADD.W    D1,D1
	BSET     #$0,D1
	BCC.S    UnsignedDivideLongWordByLongWordLoopBody
UnsignedDivideLongWordByLongWordEnd:	
	;; restore d2 and d3 (don't need to restore d1.w)
	MOVE.L   (A7)+,D3
	MOVE.W   (A7)+,D2
	RTS
